package com.example.controller;

import com.example.pojo.Brand;
import com.example.pojo.User;
import com.example.pojo.PageResult;
import com.example.pojo.Result;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 普通用户管理控制器
 */
@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询普通所有用户的名字 -GET http://localhost:8881/users/username
     */
    @GetMapping("/username")
    public Result getUsername() {
        log.info("查询普通用户名字");
        List<String> username = userService.getUsername();
        return Result.success(username);
    }


    /**
     * 分页查询 -GET http://localhost:8881/users?page=1&pageSize=10
     */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询普通用户，当前页码为：{}，每页记录数：{}",page,pageSize);
        PageResult pageResult = userService.page(page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 新增普通用户 -POST http://localhost:8881/users
     */
    @PostMapping
    public Result save(@RequestBody User user) {
        log.info("新增普通用户，普通用户数据为：{}", user);
        userService.save(user);
        return Result.success();
    }

    /**
     * 根据ID删除 -DELETE http://localhost:8881/users?userID=1
     */
    @DeleteMapping
    public Result delete(Integer userID) {
        log.info("根据ID删除普通用户，id为：{}", userID);
        userService.deleteById(userID);
        return Result.success();
    }

    /**
     * 根据ID查询 -GET http://localhost:8881/users/1
     */
    @GetMapping("/{userID}")
    public Result getById(@PathVariable Integer userID) {
        log.info("根据ID查询普通用户，id为：{}", userID);
        User user = userService.getById(userID);
        return Result.success(user);
    }

    /**
     * 修改 -PUT http://localhost:8881/users
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("修改普通用户信息:{}",user);
        userService.update(user);
        return Result.success();
    }
}
